<?php


namespace app\api\model;


use app\common\library\IDMan;
use app\common\model\BaseModel;
use common;

class SharePayRecordModel extends BaseModel
{
    protected $name = 'pay_profitshare_order';
    protected $pk = 'id';

    function add_order($city_id,$orgin_id,$total_amount,$receiver,$order_type,$gate_way,$trans_id,$pay_order_id,$share_id,$order_state)
    {
        $data = [
            'id' => IDMan::order_id(),
            'city_id' => $city_id,
            'transaction_id' => $trans_id,
            'gateway' => $gate_way,
            'origin_order_id' => $orgin_id,
            'order_type' => $order_type,
            'pay_order_id' => $pay_order_id,
            'create_time' => date('Y-m-d H:i:s'),
            'refund_time' =>date('Y-m-d H:i:s'),
            'account_id' => $receiver['account'],
            'account_name' => $receiver['desc'],
            'amount' => $receiver['amount'],
            'total_amount'=>$total_amount,
            'order_state' => $order_state
        ];

        if (!empty($share_id)){
            $data['share_id'] = $share_id;
        }

       self::create($data,true);
    }

    function query_by_origin_id($id)
    {
        $selectlist = [
            'id' => 'string',
            'transaction_id' => 'string',
            'origin_order_id' => 'string',
            'order_type' => 'string',
            'gateway' => 'string',
            'pay_order_id' => 'string',
            'create_time' => 'string',
            'refund_time' => 'string',
            'share_id' => 'string',
            'account_id' => 'string',
            'account_name' => 'string',
            'amount' => 'int',
            'total_amount'=>'int',
            'order_state' => 'int',
            'refund_amount_total' => 'int'
        ];

        $beans = [
            'origin_order_id' =>$id,
        ];
        $rows = $this->where($beans)->field(common::array_keys($selectlist))->select();
        if ($rows->isEmpty()) {
            return null;
        }

        return $rows->toArray();
    }

    //更新退款记录
    function update_profit_refund($id,$order_state,$refund_amount,$refund_total,$refund_order_id)
    {
        $data =[
            'refund_amount' => $refund_amount,
            'refund_amount_total' => $refund_total,
            'refund_order_id' => $refund_order_id,
            'refund_time' =>date('Y-m-d H:i:s'),
            'order_state' => $order_state
        ];

        self::update($data,['id' => $id]);
    }

    function query_list($cd,$pos, $limit)
    {
        $selectlist = [
            'id' => 'string',                   //id
            'city_id' => 'string',              //城市id
            'transaction_id' => 'string',       //交易单号
            'origin_order_id' => 'string',      //原始订单id
            'order_type' => 'string',           //交易类型 USER_CHARGE 充值 MEMBERSHIP 会员 USER_RIDE_CHARGE骑行支付 RIDECARD 骑行卡
            'gateway' => 'string',              //支付方式 WXPAY ALIPAY
            'pay_order_id' => 'string',         //商户订单号
            'create_time' => 'string',          //创建时间
            'refund_time' => 'string',          //退款时间
            'share_id' => 'string',             //支付宝微信订单号
            'account_id' => 'string',           //商户id
            'account_name' => 'string',         //分账描述
            'amount' => 'int',                  //分账钱
            'total_amount'=>'int',              //总充值
            'order_state' => 'int',              // 100分账成功 200退款分账
            'refund_amount' => 'int',           //退款金额
            'refund_amount_total' => 'int'      //退款总金额
        ];

        $beans = [
        ];

        //城市
        if (isset($cd['city_id'])) {
            if ($cd['city_id'] == 'ALL'){

            }
            else{
                $beans['city_id'] = $cd['city_id'];
            }
        }

        //方式
        if (isset($cd['order_type'])) {
            $beans['order_type'] = $cd['order_type'];
        }

        //order_state
        if (isset($cd['order_state'])) {
            $beans['order_state'] = $cd['order_state'];
        }

        //gateway
        if (isset($cd['gateway'])) {
            $beans['gateway'] = $cd['gateway'];
        }

        //开始时间
        if (isset($cd['begin_time'])){
            $beans['create_time'] = ['>=',$cd['begin_time']];
        }

        if (isset($cd['end_time'])){
            $beans['create_time'] = ['create_time',$cd['end_time']];
        }
        $rows = $this->where($beans)->field(common::array_keys($selectlist))->order('create_time','desc')->limit($pos,$limit)->select();
        if ($rows->isEmpty())
            return [];

        return $rows->toArray();
    }
}